<?php

namespace app\admin\controller;
use think\Controller;
use app\admin\model\Foregroundmenu;
use think\facade\Config;

class IndexBase extends Controller{
    //用户信息变量配置
    protected $_userCfg;
    //请求方式 get|post
    protected $method;
    //是否是ajax请求 true|false
    protected $isAjax;
    //页面标题
    protected $title;

    protected $param = [];
    public $com_id = 0;

    protected function initialize()
    {
        //检查登录
        $this->checkLoginStatus();

        $this->initParams();
    }

    /*
     * 初始化参数
     */
    protected function initParams(){
        $curr_day = date('j');
        if($curr_day>10){
            $this->param['default_month'] = date('Y-m-d');
        }else{
            $this->param['default_month'] = date('Y-m-d', strtotime('-1 month'));
        }
    }

    protected function getSysParam($key, $val='', $format='Y-m'){
        if($val!=''){
            cookie($key,$val);
            return date($format, strtotime($val));
        }
        $value = cookie($key);
        if(empty(cookie($key))){
            $value = $this->param[$key];
            cookie($key,$value);
        }
        return date($format, strtotime($value));
    }



    /**
     * 获取最简链接
     * @author carl.zhou 2017-08-25
     * @param $path
     * @return mixed|string
     */
    protected function getPurePath($path){
        $path = preg_replace('/\..*?$/', '', $path);
        $temp = explode('/', $path);
        do{
            $act = strtolower(array_pop($temp));
            if ($act !== 'index' && $act!=='') {
                array_push($temp, $act);
                break;
            }
        }
        while (count($temp)>0);
        $path = implode('/', $temp);
        if ($path=='') {
            $path = '/';
        }
        return $path;
    }



    /**
     * 根据登陆状态跳转
     */
    protected function checkLoginStatus(){
        //获取用户信息config
        $this->_userCfg = config('auth.login_user');


        //检查是否登录
        $login = $this->isLogin();

        $module = $this->request->module();
        $controller = $this->request->controller();
        $action = $this->request->action();
        $url = $module."/".$controller."/".$action;
        if (!$login) {
            if($url == 'admin/Index/login'){
                return $this->redirect('Index/login');
            }else{
                $this->error('您还未登录或是您的登录信息已过期', 'admin/index/login' );
            }
        }
    }

    /**
     * 判断是否登录
     * @author Rocky Huang 2017-06-28
     * @return  boolean    [description]
     */
    private function isLogin()
    {

        if (!session($this->_userCfg['name'] )) {

            if($_SERVER['REQUEST_URI']){
                redirect($_SERVER['REQUEST_URI'])->remember();
            }
            return false;
        }
        return true;
    }

    public function ajaxSuccess($msg='成功', array $data=[]){
        if(!empty($data)){
            $data['message'] = $msg;
            return json($data);
        }
        return json(['status'=>0,'message'=>$msg]);
    }

    public function ajaxError($msg='失败'){
        return json(['status'=>-1,'message'=>$msg]);
    }

    public function showError($msg, $url=''){
        if($this->isAjax){
            $data = ['message' => $msg,];
            return json($data);
        }
    }
}